package hate.util

import com.google.zxing.BarcodeFormat
import com.google.zxing.EncodeHintType
import com.google.zxing.MultiFormatWriter
import com.google.zxing.client.j2se.MatrixToImageWriter
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel
import com.itextpdf.text.*
import com.itextpdf.text.pdf.*
import java.awt.image.BufferedImage
import java.io.*
import java.util.HashMap
import javax.imageio.ImageIO

object VisaPdf {

    // 解决中文问题
    private val bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
            BaseFont.NOT_EMBEDDED)
    var data = mutableMapOf<String, Any?>()
    val file = File("c:/test/materialPdf.pdf")
    val doc = Document(PageSize.A4)
    private val font15 = Font(bfChinese, 15f, Font.BOLD)

    fun initData(){
        data["title"]="材料统计"
        data["name"]="天平"
        data["code"]="H001"
        data["amount"]=1200
        data["time"]="20190612"
    }

    fun drawPdf(){

        //标题
        val title = Paragraph("\n"+data["title"]?.toString(), font15)
        title.leading = 20f
        title.alignment = Element.ALIGN_CENTER
        doc.add(title)

        val table = PdfPTable(4)
        table.horizontalAlignment = Element.ALIGN_CENTER
        val columnWidths = floatArrayOf(40f, 60f, 40f, 60f)
        table.setTotalWidth(columnWidths)

        for(i in 1..100  step 2){

            val p1 = Paragraph(data["code"]?.toString(), font15)
            val c1 = PdfPCell(p1)

            val p2 = Paragraph(data["name"]?.toString(), font15)
            val c2 = PdfPCell(p2)

            val p3 = Paragraph(data["amount"]?.toString(), font15)
            val c3 = PdfPCell(p3)

            val p4 = Paragraph(data["time"]?.toString(), font15)
            val c4 = PdfPCell(p4)

            c1.verticalAlignment = Element.ALIGN_MIDDLE
            c2.verticalAlignment = Element.ALIGN_MIDDLE
            c3.verticalAlignment = Element.ALIGN_MIDDLE
            c4.verticalAlignment = Element.ALIGN_MIDDLE
            table.addCell(c1)
            table.addCell(c2)
            table.addCell(c3)
            table.addCell(c4)

        }

        doc.add(table)
    }

    /**
     * 加水印
     */
    fun addOverContent(file:File,code:String){

        //file水印二维码上去哦
        val pdfReader = com.itextpdf.text.pdf.PdfReader(FileInputStream(file))
        val stamper = PdfStamper(pdfReader, FileOutputStream(file))
        val over = stamper.getOverContent(1)

       // val imgPath = VisaPdf.genQCodeJson(code)
        val image = Image.getInstance("")

        // 图片位置
        image.setAbsolutePosition(420f, 750f)
        // 设定图片尺寸
        image.scaleAbsolute(200f, 200f)

        // 设定图片比例
        image.scalePercent(40f)

        // 添加图片
        over.addImage(image)
        stamper.close()
        File(file.path)
    }


    /**
     * 根据内容，和大小来生成固定二维码
     * 生成二维码
     */
    @JvmStatic
    @Throws(Exception::class)
    fun genQrCode(content: String, size: Int): BufferedImage {
        val hints = HashMap<EncodeHintType, Any>()
       // Map<com.google.zxing.EncodeHintType, ?>
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8")
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M)
        val matrix = MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, size, size, hints)
        val bos = ByteArrayOutputStream()
        MatrixToImageWriter.writeToStream(matrix, "jpg", bos)
        val bais = ByteArrayInputStream(bos.toByteArray())
        val qcodeImage = ImageIO.read(bais)


        return qcodeImage
    }


    fun addImg2Pdf(){
        //创建PdfDocument类的对象
        var pdf = PdfDocument()

        //加载测试文档
        //pdf.loadFromFile("test.pdf")
        //获取测试文档中的第一页
       // PdfPageBase page = pdf.getPages().get(0);
        //调用insertWatermark()方法添加文本水印
       // insertWatermark(page, "TOP SECRET");
        //保存文档
        //pdf.saveToFile("out/textWaterMark.pdf");

    }

    @JvmStatic
    fun main(args: Array<String>) {

        var img=genQrCode("二维码测试",198)

        val outputfile = File("C:\\test\\saved.png")
        ImageIO.write(img, "png", outputfile)


        /*initData()
        PdfWriter.getInstance(doc, FileOutputStream(file))
        doc.open()
        drawPdf()
        doc.close()

        println(file.absolutePath)*/
    }

}